package com.yutel.phone.xplayer.service;

import java.net.InetAddress;

import android.content.Context;
import android.content.Intent;

import com.yutel.phone.xplayer.ui.PlayerActivity;
import com.yutel.silver.Aika;
import com.yutel.silver.Aika.AikaConnectListener;
import com.yutel.silver.exception.AirplayException;
import com.yutel.silver.util.StringUtil;
import com.yutel.silver.vo.Device;

public class AikaServer implements AikaConnectListener {
	private static Device mDevice;
	private static AikaServer singleServer;

	private Context mContext;
	private Aika mAika;
	private InetAddress mIP;
	private int mPort;
	private String mName;

	private AikaServer(Device device, InetAddress ip, int port, String name) {
		mDevice = device;
		mIP = ip;
		mPort = port;
		mName = name;
	}

	public void setContext(Context context) {
		mContext = context;
	}

	public void start() {
		mAika = Aika.create(mIP, mPort, mName);
		// connect listener is must fist
		mAika.setConnectListener(this);
		// device info
		mAika.config(mDevice);
		mAika.start();
	}

	public Aika getAika() {
		return mAika;
	}

	public void stop() {
		mAika.stop();
	}

	public static AikaServer getInstance() {
		if (singleServer == null) {
			System.out.println("AikaServer is init or started,please wait!");
		}
		return singleServer;
	}

	public static AikaServer getInstance(Device device, InetAddress ip,
			int port, String name) {
		if (singleServer == null) {
			singleServer = new AikaServer(device, ip, port, name);
		}
		return singleServer;
	}

	@Override
	public void video(String url, String rate, String pos)
			throws AirplayException {
		System.out.println("video====" + url + ",pos");
		Intent video = new Intent(mContext, PlayerActivity.class);
		video.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		video.putExtra(Intent.EXTRA_TEXT, url);
		float posf = StringUtil.toFloat(pos);
		video.putExtra("position", posf);
		mContext.startActivity(video);
	}

	@Override
	public void photo() throws AirplayException {
		System.out.println("photo()");
	}
}
